export default {
  regist: (name, passwd, mail, scb, fcb) => {
    fetch('/regist', {
      credentials: 'include',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        name: name,
        mail: mail,
        password: passwd
      })
      }).then((res) => {
        return res.json()
      }).then((json) => {
        if (json.status === 'success') {
          scb()
        } else {
          fcb()
        }
    })
  },

  login: (name, passwd, scb, fcb) => {
    fetch('/login', {
      credentials: 'include',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        name: name,
        password: passwd
      })
    }).then((res) => {
        return res.json()
      }).then((json) => {
        if (json.status === 'success') {
          scb({isAdmin: json.isAdmin === 1 ? true : false})
        } else {
          fcb()
        }
      })
  },

  logout: (scb) => {
    scb()
  },

  update: (userInfo, scb, fcb) => {
    console.log('post update:', userInfo)
    fetch('/user/update', {
      credentials: 'include',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        location: userInfo.location,
        phone: userInfo.phone,
        mail: userInfo.mail
      })
    }).then((res) => {
        return res.json()
      }).then((json) => {
        if (json.status === 'success') {
          scb()
        } else {
          fcb()
        }
      })
  },

  getInfo: (userId, scb, fcb) => {
    fetch('/viewUser', {
      credentials: 'include',
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      }
    }).then((res) => {
        return res.json()
      }).then((json) => {
        if (json) {
          scb(json)
        } else {
          fcb()
        }
      })
  }
}